import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../../app_files.dart';
import 'action.dart';
import 'state.dart';

Widget buildView(SelectCommunityViewState state, Dispatch dispatch,
    ViewService viewService) {
  Widget itemBuilder(BuildContext context, int index) {
    CommunityModel model = state.items[index];
    return Container(
      height: 45,
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: ThemeConfig.separeColor))),
      margin: EdgeInsets.only(left: 15, right: 15),
      child: InkWell(
        child: Row(
          children: [
            Expanded(
                child: Text(
              model.name,
              style: TextThemeConfig.Font15WithColor22,
            )),
            Image.asset('images/right_arrow.png'),
          ],
        ),
        onTap: ()=> Navigator.pop(context,model),
      ),
    );
  }

  return Scaffold(
    backgroundColor: Colors.white,
    appBar: myAppBar(viewService.context, title: '选择小区'),
    body: Column(
      children: [
        Container(
          color: ThemeConfig.mainColor,
          child: Container(
            margin: EdgeInsets.all(15),
            height: 40,
            padding: EdgeInsets.only(left: 14),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(
                Radius.circular(20),
              ),
            ),
            child: Row(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(bottom: 1),
                  child: Image.asset('images/icon_search.png'),
                ),
                Padding(padding: EdgeInsets.only(left: 8)),
                Expanded(
                  child: TextField(
                    style: TextStyle(fontSize: 14),
                    textInputAction: TextInputAction.search,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: '搜索小区名',
                      hintStyle: TextStyle(
                          color: ThemeConfig.black153Color, fontSize: 14),
                      contentPadding: EdgeInsets.only(bottom: 9),
                    ),
                    onSubmitted: (String text) => dispatch(
                        SelectCommunityViewActionCreator.onSearch(text)),
                    onChanged: (String text) => dispatch(
                        SelectCommunityViewActionCreator.onTextChange(text)),
                    controller: state.textEditingController,
                  ),
                ),
                Offstage(
                  offstage: !state.showClose,
                  child: InkWell(
                    child: Padding(
                      padding: EdgeInsets.only(right: 12),
                      child: Image.asset('images/ic_del.png'),
                    ),
                    onTap: () => dispatch(
                        SelectCommunityViewActionCreator.onClearText()),
                  ),
                )
              ],
            ),
          ),
        ),
        Expanded(
          child: state.items == null
              ? MyLoadingWidget()
              : CustomRefreshList(
                  onLoading: (_, page) =>
                      dispatch(SelectCommunityViewActionCreator.onLoad(page)),
                  onRefresh: (_) =>
                      dispatch(SelectCommunityViewActionCreator.onLoad(1)),
                  currentPage: state.currentPage,
                  refreshController: state.refreshController,
                  child: ListView.builder(
                    itemBuilder: itemBuilder,
                    itemCount: state.items.length,
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                  ),
                ),
        ),
      ],
    ),
  );
}
